package com.imedway.ihe.dtz.xds.registry.camel;

import java.util.ArrayList;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryError30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryErrorList;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.registry.sub.camel.Iti42SubRegistry_CAMEL;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class Iti61Registry_CAMEL {
	
	@Autowired
	private Iti42SubRegistry_CAMEL iti42SubRegistry_CAMEL;

	public RegistryResponseType run(SubmitObjectsRequest body) {
		
		RegistryResponseType registryResponseType = new RegistryResponseType();
		EbXMLSubmitObjectsRequest30 ebXMLbody= new EbXMLSubmitObjectsRequest30(body);
		registryResponseType.setRegistryErrorList(new RegistryErrorList());
		List<EbXMLRegistryError> registryError = new ArrayList<EbXMLRegistryError>();
		EbXMLRegistryResponse30 ebXMLRegistryResponse30 = new EbXMLRegistryResponse30(registryResponseType);
		try {
			
			RegisterDocumentSet registerDocumentSet = EbXML30Converters.convert(body);
//			SubmissionSet submissionSet = registerDocumentSet.getSubmissionSet();
			List<DocumentEntry> documentEntryList= registerDocumentSet.getDocumentEntries();
//			List<Folder> folderList = registerDocumentSet.getFolders();
//			List<Association> associationList = registerDocumentSet.getAssociations();
			boolean flag=false;
			for(DocumentEntry documentEntry:documentEntryList) {
				log.info(documentEntry.getType().getUuid());
				if(documentEntry.getType()!=null&&SysConsts.DocumentEntryObjectTypeForStableDocumentEntries.equals(documentEntry.getType().getUuid())) {
					flag=true;
					break;
				}
			}
			log.info(String.valueOf(flag));
			if(flag) {
				genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
						"hava no On-Demand Document Entries");	
				ebXMLRegistryResponse30.setErrors(registryError);
			}else {
				iti42SubRegistry_CAMEL.run(ebXMLRegistryResponse30,registerDocumentSet);
			}
			
										
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
					e.getMessage());	
			ebXMLRegistryResponse30.setErrors(registryError);
		}
		
		ebXMLRegistryResponse30.setErrors(registryError);
		ebXMLRegistryResponse30.setStatus(!registryError.isEmpty() ? Status.SUCCESS : Status.FAILURE);
		
		return ebXMLRegistryResponse30.getInternal();
	}
	
	private void genError(List<EbXMLRegistryError> registryError,Severity severity,String errorCode,String codeContext) {
		EbXMLRegistryError30 re = new EbXMLRegistryError30(new RegistryError());
		re.setSeverity(severity);
		re.setErrorCode(errorCode);
		re.setCodeContext(codeContext);
		registryError.add(re);
	}
}
